{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('python-codes-25k.jsonl', 'r', encoding='utf-8') as f:\n",
    "    lines = f.readlines()\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train = pd.DataFrame(columns=['data_source', 'prompt', 'ability', 'reward_model', 'extra_info'])\n",
    "df_val = pd.DataFrame(columns=['data_source', 'prompt', 'ability', 'reward_model', 'extra_info'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "INSTRUCTIONS = \"\"\"Generate code to solve the given question. \\\n",
    "You must conduct reasoning inside <think> and </think> before <code> and <answer> every time. \\\n",
    "After reasoning, if you find you need run code, you can call a code engine by <code> your code </code> and it will return the run results between <observation> and </observation>. \\\n",
    "You can generate and run code as many times as your want. \\\n",
    "After reasoning, if you find the given question has already been solved , you can directly provide the run summary inside <answer> and </answer>. \\\n",
    "Question: {}\\n\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_source = []\n",
    "prompt = []\n",
    "ability = []\n",
    "reward_model = []\n",
    "extra_info = []\n",
    "for idx, line in enumerate(lines[:2000]):\n",
    "    line = json.loads(line)\n",
    "    content = line['instruction'] + line['input']\n",
    "    data_source.append('code')\n",
    "    prompt.append([{\"role\": \"user\", \"content\": INSTRUCTIONS.format(content)}])\n",
    "    ability.append('text')\n",
    "    reward_model.append({\n",
    "                \"style\": \"rule\",\n",
    "                \"ground_truth\": \"\",\n",
    "            })\n",
    "    \n",
    "    extra_info.append({\n",
    "                'split': 'train',\n",
    "                'index': idx,\n",
    "                \"user_message\": content,\n",
    "            })\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train['data_source'] = data_source\n",
    "df_train['prompt'] = prompt\n",
    "df_train['ability'] = ability\n",
    "df_train['reward_model'] = reward_model\n",
    "df_train['extra_info'] = extra_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data_source</th>\n",
       "      <th>prompt</th>\n",
       "      <th>ability</th>\n",
       "      <th>reward_model</th>\n",
       "      <th>extra_info</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'train', 'index': 0, 'user_message':...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'train', 'index': 1, 'user_message':...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'train', 'index': 2, 'user_message':...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'train', 'index': 3, 'user_message':...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'train', 'index': 4, 'user_message':...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  data_source                                             prompt ability  \\\n",
       "0        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "1        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "2        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "3        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "4        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "\n",
       "                            reward_model  \\\n",
       "0  {'style': 'rule', 'ground_truth': ''}   \n",
       "1  {'style': 'rule', 'ground_truth': ''}   \n",
       "2  {'style': 'rule', 'ground_truth': ''}   \n",
       "3  {'style': 'rule', 'ground_truth': ''}   \n",
       "4  {'style': 'rule', 'ground_truth': ''}   \n",
       "\n",
       "                                          extra_info  \n",
       "0  {'split': 'train', 'index': 0, 'user_message':...  \n",
       "1  {'split': 'train', 'index': 1, 'user_message':...  \n",
       "2  {'split': 'train', 'index': 2, 'user_message':...  \n",
       "3  {'split': 'train', 'index': 3, 'user_message':...  \n",
       "4  {'split': 'train', 'index': 4, 'user_message':...  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train.to_parquet('./code_train.parquet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_source = []\n",
    "prompt = []\n",
    "ability = []\n",
    "reward_model = []\n",
    "extra_info = []\n",
    "for idx, line in enumerate(lines[-100:]):\n",
    "    line = json.loads(line)\n",
    "    content = line['instruction'] + line['input']\n",
    "    data_source.append('code')\n",
    "    prompt.append([{\"role\": \"user\", \"content\": INSTRUCTIONS.format(content)}])\n",
    "    ability.append('text')\n",
    "    reward_model.append({\n",
    "                \"style\": \"rule\",\n",
    "                \"ground_truth\": \"\",\n",
    "            })\n",
    "    \n",
    "    extra_info.append({\n",
    "                'split': 'val',\n",
    "                'index': idx,\n",
    "                \"user_message\": content,\n",
    "            })\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_val['data_source'] = data_source\n",
    "df_val['prompt'] = prompt\n",
    "df_val['ability'] = ability\n",
    "df_val['reward_model'] = reward_model\n",
    "df_val['extra_info'] = extra_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data_source</th>\n",
       "      <th>prompt</th>\n",
       "      <th>ability</th>\n",
       "      <th>reward_model</th>\n",
       "      <th>extra_info</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'val', 'index': 0, 'user_message': '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'val', 'index': 1, 'user_message': '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'val', 'index': 2, 'user_message': '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'val', 'index': 3, 'user_message': '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>code</td>\n",
       "      <td>[{'role': 'user', 'content': 'Generate code to...</td>\n",
       "      <td>text</td>\n",
       "      <td>{'style': 'rule', 'ground_truth': ''}</td>\n",
       "      <td>{'split': 'val', 'index': 4, 'user_message': '...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  data_source                                             prompt ability  \\\n",
       "0        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "1        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "2        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "3        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "4        code  [{'role': 'user', 'content': 'Generate code to...    text   \n",
       "\n",
       "                            reward_model  \\\n",
       "0  {'style': 'rule', 'ground_truth': ''}   \n",
       "1  {'style': 'rule', 'ground_truth': ''}   \n",
       "2  {'style': 'rule', 'ground_truth': ''}   \n",
       "3  {'style': 'rule', 'ground_truth': ''}   \n",
       "4  {'style': 'rule', 'ground_truth': ''}   \n",
       "\n",
       "                                          extra_info  \n",
       "0  {'split': 'val', 'index': 0, 'user_message': '...  \n",
       "1  {'split': 'val', 'index': 1, 'user_message': '...  \n",
       "2  {'split': 'val', 'index': 2, 'user_message': '...  \n",
       "3  {'split': 'val', 'index': 3, 'user_message': '...  \n",
       "4  {'split': 'val', 'index': 4, 'user_message': '...  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_val.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_val.to_parquet('./code_val.parquet')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "wyf",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
